home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPsdk_40.lha / AmiTCP-4.0 / src / netlib / autoinit.c < prev    next >
C/C++ Source or Header  |  1994-09-29  |  5KB  |  167 lines

  1. RCS_ID_C="$Id: autoinit.c,v 4.3 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      autoinit.c - SAS/C autoinitialization functions for bsdsocket.library
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/libraries.h>
  12.  
  13. #include <intuition/intuition.h>
  14.  
  15. #include <proto/socket.h>
  16. #include <proto/exec.h>
  17. #include <proto/intuition.h>
  18.  
  19. #include <stdlib.h>
  20.  
  21. #include <errno.h>
  22.  
  23. #include <amitcp/socketbasetags.h>
  24.  
  25. struct Library *SocketBase = NULL;
  26.  
  27. static const char SOCKETNAME[] = "bsdsocket.library";
  28.  
  29. #define SOCKETVERSION 4        /* minimum bsdsocket version to use */
  30.  
  31. extern STRPTR _ProgramName;    /* SAS startup module defines this :-) */
  32.  
  33. /*
  34.  * Globa| h_errno
  35.  */
  36. #if sizeof(int) != sizeof(long)
  37. #error short ints not supported!
  38. #endif
  39. int h_errno = 0;
  40.  
  41. /****** net.lib/autoinit *********************************************
  42.  
  43.     NAME
  44.         autoinit - SAS C Autoinitialization Functions
  45.  
  46.     SYNOPSIS
  47.         LONG _STI_200_openSockets(void)
  48.  
  49.         void _STD_200_closeSockets(void)
  50.  
  51.     FUNCTION
  52.         These functions open and close the bsdsocket.library at the startup
  53.         and exit of the program, respectively.  For a program to use these
  54.         functions, it must be linked with netlib:net.lib (or some variant).
  55.         These functions are linked in only if the program references the
  56.         global symbol "SocketBase".
  57.  
  58.         If the library can be opened, the _STI_200_openSockets() calls
  59.         bsdsocket.library function SocketBaseTags() to tell the library the
  60.         address and the size of the errno variable of the calling program,
  61.         the program name (to be used in syslog() messages) and the address
  62.         of the h_error variable (in which the name resolver errors are
  63.         returned).
  64.  
  65.     NOTES
  66.         _STI_200_openSockets() also checks that the system version is at
  67.         least 37. It also puts up a requester if the bsdsocket.library is
  68.         not found or is of wrong version.
  69.  
  70.         The autoinitialization and autotermination functions are features
  71.         specific to the SAS C6.  However, these functions can be used with
  72.         other (ANSI) C compilers, too. Example follows:
  73.  
  74.         \* at start of main() *\
  75.  
  76.         atexit(_STD_200_closeSockets);
  77.         if (_STI_200_openSockets() != 0)
  78.        exit(20);
  79.  
  80.     BUGS
  81.         The same autoinitialization won't work for both SAS C 6.3 and SAS C
  82.         6.50 or latter.  Only way to terminate an initialization function is
  83.         by exit() call with SAS C 6.3 binary.  If an autoinitialization
  84.         function is terminated by exit() call with SAS C 6.50 binary, the
  85.         autotermination functions won't be called.  Due this braindamage
  86.         these compilers require separate net.lib libraries.
  87.  
  88.     SEE ALSO
  89.         bsdsocket.library/SocketBaseTags(),
  90.         SAS/C 6 User's Guide p. 145 for details of autoinitialization and
  91.         autotermination functions.
  92.  
  93. *****************************************************************************
  94. */
  95.  
  96. /* SAS C 6.50 kludge */
  97. #if __VERSION__ > 6 || __REVISION__ >= 50
  98. #define exit(x) return(x)
  99. #endif
  100.  
  101. /*
  102.  * Using __stdargs prevents creation of register arguments entry point.
  103.  * If both stack args and reg. args entry points are created, this
  104.  * function is called _twice_, which is not wanted.
  105.  *
  106.  * The number 200 in the function names is the priority assigned to
  107.  * shared library autoinitialization functions by SAS/C 6.50.
  108.  */
  109. LONG __stdargs
  110. _STI_200_openSockets(void)
  111. {
  112.   struct Library *IntuitionBase;
  113.   STRPTR errorStr;
  114.  
  115.   /*
  116.    * Check OS version
  117.    */
  118.   if ((*(struct Library **)4)->lib_Version < 37)
  119.     exit(20);
  120.  
  121.   /*
  122.    * Open bsdsocket.library
  123.    */
  124.   if ((SocketBase = OpenLibrary((STRPTR)SOCKETNAME, SOCKETVERSION)) != NULL) {
  125.     /*
  126.      * Succesfull. Now tell bsdsocket.library:
  127.      * - the address of our errno
  128.      * - the address of our h_errno
  129.      * - our program name
  130.      */
  131.     if (SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), &errno,
  132.                SBTM_SETVAL(SBTC_HERRNOLONGPTR), &h_errno,
  133.                SBTM_SETVAL(SBTC_LOGTAGPTR), _ProgramName,
  134.                TAG_END))
  135.       exit(30);
  136.     return 0;
  137.   }
  138.   else
  139.     errorStr = "AmiTCP/IP version 4 or later must be started first.";
  140.  
  141.   IntuitionBase = OpenLibrary("intuition.library", 36);
  142.  
  143.   if (IntuitionBase != NULL) {
  144.     struct EasyStruct libraryES;
  145.  
  146.     libraryES.es_StructSize = sizeof(libraryES);
  147.     libraryES.es_Flags = 0;
  148.     libraryES.es_Title = _ProgramName;
  149.     libraryES.es_TextFormat = errorStr;
  150.     libraryES.es_GadgetFormat = "Exit %s";
  151.  
  152.     EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);
  153.  
  154.     CloseLibrary(IntuitionBase);
  155.   }
  156.   exit(20);
  157. }
  158.  
  159. void __stdargs
  160. _STD_200_closeSockets(void)
  161. {
  162.   if (SocketBase) {
  163.     CloseLibrary(SocketBase);
  164.     SocketBase = NULL;
  165.   }
  166. }
  167.